﻿'
' DotNetNuke® - http://www.dotnetnuke.com
' Copyright (c) 2002-2009
' by DotNetNuke Corporation
'
' Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
' documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
' the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
' to permit persons to whom the Software is furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in all copies or substantial portions 
' of the Software.
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
' DEALINGS IN THE SOFTWARE.
'
Imports DotNetNuke.Entities.Modules
Imports DotNetNuke.Entities.Modules.Actions

Imports DotNetNuke.Modules.Announcements.Components.BaseClasses

Partial Public Class Dispatcher
    Inherits AnnouncementsBase
    Implements IActionable



    Protected m_controlToLoad As String
    Protected m_LoadedControl As AnnouncementsBase

    Protected ReadOnly Property DefaultControl() As String
        Get
            Return "Announcements.ascx"
        End Get
    End Property

    Protected Sub LoadControlType()

        If m_controlToLoad <> "" Then

            m_LoadedControl = CType(Me.LoadControl(m_controlToLoad), AnnouncementsBase)

            If Not (m_LoadedControl Is Nothing) Then

                m_LoadedControl.ModuleConfiguration = Me.ModuleConfiguration
                m_LoadedControl.ID = System.IO.Path.GetFileNameWithoutExtension(m_controlToLoad)
                m_LoadedControl.Dispatcher = Me
                Controls.Add(m_LoadedControl)

            End If
        End If

    End Sub

    Protected Sub ReadQueryString()
        If Not (Request("viewType") Is Nothing) Then

            ' Load the appropriate Control
            '
            Select Case Request("viewType").ToLowerInvariant()

                Case "view"
                    m_controlToLoad = "Announcements.ascx"
                Case "edit", "review"
                    m_controlToLoad = "EditAnnouncements.ascx"
                Case "publish"
                    ' verify security 
                    If Me.IsEditable = True And PortalSettings.UserMode = DotNetNuke.Entities.Portals.PortalSettings.Mode.Edit AndAlso Request.QueryString("itemid") IsNot Nothing Then
                        ' get content
                        Dim objHTML As New Components.Business.AnnouncementsController
                        Dim WorkflowID As Integer = Workflow.WorkflowSettingsController.GetWorkflow(ModuleId, TabId, PortalId).Value
                        Dim objContent As Components.Business.AnnouncementInfo = objHTML.GetTopAnnouncement(ModuleId, False, WorkflowID, Integer.Parse(Request.QueryString("itemid")))

                        Dim objWorkflow As New Workflow.WorkflowStateController
                        If objContent.StateID = objWorkflow.GetFirstWorkflowStateID(WorkflowID) Then
                            ' publish content
                            objContent.StateID = objWorkflow.GetNextWorkflowStateID(objContent.WorkflowID, objContent.StateID)

                            ' save the content
                            objHTML.UpdateAnnouncement(objContent, Me.UserId, Workflow.WorkflowSettingsController.GetMaximumVersionHistory(PortalId))

                            ' add log history
                            Dim objLog As New Workflow.WorkflowLogInfo
                            objLog.ModuleID = ModuleId
                            objLog.ItemID = objContent.ItemId
                            objLog.StateID = objContent.StateID
                            objLog.Approved = objContent.Approved
                            objLog.Comment = objContent.Comment

                            Dim objLogs As New Workflow.WorkflowLogController
                            objLogs.Add(objLog)

                            Dim objAnnouncements As New Components.Business.AnnouncementsController
                            objAnnouncements.DeleteAnnouncementUsers()
                            objContent = objAnnouncements.GetAnnouncement(objContent.ItemId, objContent.ModuleId)

                            ' create user notifications
                            objLogs.CreateUserNotifications(PortalId, objContent.ModuleId, objContent.ItemId, objContent.CreatedByUserID, objContent.WorkflowID, objContent.StateID, objContent.StateName, objContent.IsPublished, objContent.Notify)

                            ' refresh output cache
                            ModuleController.SynchronizeModule(ModuleId)

                            ' refresh page
                            Response.Redirect(NavigateURL(), True)
                        End If
                    End If
                Case Else

                    m_controlToLoad = DefaultControl

            End Select

        Else

            ' Type parameter not found, load default control
            '
            m_controlToLoad = DefaultControl

        End If

    End Sub

    Public Sub AddMessage(ByVal message As String, ByVal MessageType As Skins.Controls.ModuleMessage.ModuleMessageType)
        Skins.Skin.AddModuleMessage(Me, message, MessageType)

    End Sub

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init

        ' Assign parameters
        '
        ReadQueryString()

        ' Load appropriate type
        '
        LoadControlType()

        If (m_controlToLoad <> "") AndAlso (m_controlToLoad <> DefaultControl) Then
            Dim strNewModuleTitle As String = Localization.GetString(m_controlToLoad, LocalResourceFile)
            If Not String.IsNullOrEmpty(strNewModuleTitle) Then ModuleConfiguration.ModuleTitle = strNewModuleTitle
        End If


    End Sub

    Public ReadOnly Property ModuleActions() As Entities.Modules.Actions.ModuleActionCollection Implements Entities.Modules.IActionable.ModuleActions
        Get
            Dim Actions As New Entities.Modules.Actions.ModuleActionCollection

            ' add actions from current showing control
            If Not m_LoadedControl Is Nothing Then
                If TypeOf (m_LoadedControl) Is IActionable Then
                    If Not CType(m_LoadedControl, IActionable).ModuleActions Is Nothing Then
                        For Each ma As ModuleAction In CType(m_LoadedControl, IActionable).ModuleActions
                            Actions.Add(GetNextActionID, ma.Title, ma.CommandName, ma.CommandArgument, ma.Icon, ma.Url, ma.UseActionEvent, ma.Secure, ma.Visible, ma.NewWindow)
                        Next
                    End If
                End If
            End If

            Return Actions

        End Get
    End Property
End Class